package com.markbuikema.juliana32.adapter;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.markbuikema.juliana32.R;
import com.markbuikema.juliana32.model.teams.Event;
import com.markbuikema.juliana32.model.teams.FacebookEvent;
import com.markbuikema.juliana32.util.Constants;
import com.markbuikema.juliana32.util.KnvbDataManager;
import com.markbuikema.juliana32.util.KnvbDataManager.KnvbCallback;
import com.markbuikema.juliana32.util.KnvbParser;
import com.markbuikema.juliana32.util.facebook.FacebookManager;
import com.origamilabs.library.views.StaggeredGridView;

public class AgendaWeekPagerAdapter extends PagerAdapter {

	private Context context;

	public AgendaWeekPagerAdapter(Context context) {
		this.context = context;
	}

	@Override
	public int getCount() {
		return Constants.WEEKS_IN_YEAR;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {

		final int week = position;

		LayoutInflater inflater = LayoutInflater.from(context);
		View root = inflater.inflate(R.layout.fragment_agenda_week, container, false);

		final StaggeredGridView gridView = (StaggeredGridView) root.findViewById(R.id.agenda_week_list);
		final ProgressBar loading = (ProgressBar) root.findViewById(R.id.agenda_week_loading);
		final TextView emptyState = (TextView) root.findViewById(R.id.agenda_week_empty);

		loading.setVisibility(View.VISIBLE);
		emptyState.setVisibility(View.GONE);

		final AgendaEventAdapter adapter = new AgendaEventAdapter(context);
		gridView.setAdapter(adapter);

		HashMap<String, String> args = new HashMap<String, String>();
		args.put("weeknummer", Integer.toString(week));

		KnvbDataManager.getInstance().request("/wedstrijden", args, new KnvbCallback() {

			@Override
			public void onDataReceived(String json) {
				final List<Event> events = new ArrayList<Event>();
				events.addAll(KnvbParser.parseGames(json));

				new AsyncTask<Void, Void, List<FacebookEvent>>() {

					@Override
					protected List<FacebookEvent> doInBackground(Void... params) {
						return FacebookManager.getInstance().getJulianaEvents(week);
					}

					protected void onPostExecute(List<FacebookEvent> result) {
						events.addAll(0, result);
						adapter.setEvents(events);
						emptyState.setVisibility(adapter.isEmpty() ? View.VISIBLE : View.GONE);
						emptyState.setText(week < new GregorianCalendar().get(GregorianCalendar.WEEK_OF_YEAR) ? R.string.no_events_too_early : R.string.no_events_too_late);
						loading.setVisibility(View.GONE);
					}
				}.execute();
			}
		});

		((ViewPager) container).addView(root);
		return root;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

}
